#!/bin/sh /etc/rc.common
# Copyright (C) 2020 IrineSistiana

START=91
USE_PROCD=1

#####  ONLY CHANGE THIS BLOCK  ######
PROG=/usr/bin/mosdns # where is mosdns
RES_DIR=/etc/mosdns/ # resource dir / working dir / the dir where you store ip/domain lists
CONF=./config.yaml   # where is the config file, it can be a relative path to $RES_DIR
CRON_FILE=/etc/crontabs/root
#####  ONLY CHANGE THIS BLOCK  ######

inital_conf() {
  config_load "mosdns"
  config_get "enabled" "mosdns" "enabled" "0"
}
service_triggers() {
  procd_add_reload_trigger "mosdns"
}

restore_setting() {
  redirect=$(uci -q get mosdns.mosdns.redirect)
  if [ $redirect -eq 1 ]; then
    sed -i "/list server/d" /etc/config/dhcp
    uci set dhcp.@dnsmasq[0].noresolv='0'
    uci del dhcp.@dnsmasq[0].cachesize
    uci set dhcp.@dnsmasq[0].filter_aaaa='1'
    uci commit dhcp
  fi
}

prepare_setting() {
  redirect=$(uci -q get mosdns.mosdns.redirect)
  if [ $redirect -eq 1 ]; then
    sed -i "/list server/d" /etc/config/dhcp
    uci add_list dhcp.@dnsmasq[0].server='127.0.0.1#5335'
    uci set dhcp.@dnsmasq[0].rebind_protection='0'
    uci set dhcp.@dnsmasq[0].noresolv="1"
    uci set dhcp.@dnsmasq[0].cachesize='0'
    uci set dhcp.@dnsmasq[0].filter_aaaa='0'
    uci commit dhcp
  fi
}

restart_others() {
  /etc/init.d/network reload
  /etc/init.d/dnsmasq reload
}

reload_service() {
  stop
  sleep 2s
  echo "MosDNS is restarted!"
  start
}

setcron() {
  chmod 755 /etc/mosdns/mosupdater.sh
  [ "$(tail -n1 /etc/crontabs/root | wc -l)" -eq 0 ] && [ -n "$(cat /etc/crontabs/root 2>/dev/null)" ] && echo >>/etc/crontabs/root
  [ -z "$(grep "mosupdater.sh" "$CRON_FILE" 2>/dev/null)" ] && {
    [ "$(uci -q get mosdns.mosdns.geo_auto_update)" -eq 1 ] && echo "0 $(uci -q get mosdns.mosdns.geo_update_day_time) * * $(uci -q get mosdns.mosdns.geo_update_week_time) /etc/mosdns/mosupdater.sh" >>$CRON_FILE
  }
  crontab $CRON_FILE
}

delcron() {
  sed -i '/mosupdater.sh/d' $CRON_FILE 2>/dev/null
  /etc/init.d/cron restart
}

start_service() {

  # Reading config
  inital_conf
  if [ "$enabled" -eq 0 ]; then
    firstblood=$(uci -q get mosdns.mosdns.not_first_start)
    [ "${firstblood}" = "0" ] && restore_setting
    [ "${firstblood}" = "0" ] && restart_others
    uci set mosdns.mosdns.not_first_start='1'
    uci commit mosdns
    echo "MosDNS has turned off"
    return 1
  fi
  delcron
  setcron
  procd_open_instance mosdns
  procd_set_param command $PROG -dir $RES_DIR -c $CONF
  procd_set_param user root
  procd_set_param stdout 1
  procd_set_param stderr 1
  procd_set_param respawn "${respawn_threshold:-3600}" "${respawn_timeout:-5}" "${respawn_retry:-5}"
  procd_close_instance mosdns

  prepare_setting
  restart_others

  echo "MosDNS turn on"
  echo "enabled="$enabled""

}

stop_service() {

  killall -q mosdns
  echo "MosDNS turn off"
  echo "enabled="$enabled""

  config_load "mosdns"
  enabled=$(uci -q get mosdns.mosdns.enabled)
  [ "${enabled}" = "0" ] && restore_setting
  restart_others
  delcron

}
